<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦
 * @Email:ikphp@sina.cn 2014年8月24日18:48:50
 * 爱客网全站用户积分
 */
namespace IKPHP\Plugins\UserScore;

use IKPHP\Plugins\BasePlugin;
use IKPHP\Common\Models\User;
use IKPHP\Common\Models\UserScore;
use IKPHP\Common\Models\UserScoreLog;
use IKPHP\Common\Models\UserLevel;

class UserScorePlugin extends BasePlugin {
	
    public function changeScore($params){
    	$arr_setting = array();
    	$cacheKey = 'scorerule';
        $cache   = $this->getDI()->getShared('modelCache'); 
        $scoreRule = $cache->get($cacheKey); 
        if($scoreRule == null){ 
            $res = UserScore::find();  
            $cache->save($cacheKey, $res);
            $scoreRule = $res;
        }
        
        foreach ($scoreRule as $item) {
        	$arr_setting['ik_'.$item->action] = $item->toArray();
        }      
        //第二中方法配置C方法
		C($arr_setting);          
    	$this->_alter_score($params);
    }
    /**
     * 改变用户积分
     * 配置操作行为必须和标签名称一致
     */
    private function _alter_score($_data) {
        $arrAction = C('ik_'.$_data['action']); //获取积分变量
        $score = intval($arrAction['score']);
        
        if (intval($score) == 0) return false; //积分为0
        if ($this->_check_num($_data['uid'], $_data['action'])) {
        	//获取当前积分
        	$strUser = User::findFirst(array('columns'=>'count_score,roleid',"userid = ".$_data['uid']));
        	$cutcore = intval($strUser->count_score);
        	$newscore = $cutcore + $score > 0 ? $cutcore + $score : 0 ;//新的积分
        	
        	User::findFirst(array("userid = ".$_data['uid']))->save(array('count_score'=>$newscore)); //改变用户积分
        	
        	$userRole = new UserLevel();
            $roleRes = $userRole->getRoleByScore($newscore);
            
            if($strUser->roleid != $roleRes['roleid']){
            	//如果不相同 改变用户角色id 
            	User::findFirst("userid = ".$_data['uid'])->save(array('roleid'=>$roleRes['roleid']));
            }
            //积分日志
            $score_log_mod = new UserScoreLog();
            $score_log_mod->create(array(
                'uid' => $_data['uid'],
                'uname' => $_data['uname'],
                'action' => $_data['action'],
            	'actionname' => $_data['actionname'],
                'score' => $score,
            	'add_time' => time(),
            ));
        }
    }

    /**
     * 检查次数限制
     */
    private function _check_num($uid, $action){
        $return = false;
        $user_stat_mod = M('UserStat'); 
        //登陆次数限制
        $arrAction = C('ik_'.$action); //获取积分变量
        $max_num = $arrAction['maxnum'];
        //先检查统计信息
        $stat = $user_stat_mod->findFirst("uid = '$uid' and action = '$action'"); 
        if (!$stat) {
            $user_stat_mod->create(array('uid'=>$uid, 'action'=>$action,'num'=>0,'last_time'=>time()));
            $stat = $user_stat_mod->findFirst("uid = '$uid' and action = '$action'"); 
        }
        
        $new_num = $stat->num + 1;
        
        if ($max_num == 0) {
            $return = true; //为0则不限制
        } else {
        	
            if ($stat->last_time < todaytime()) { 
                $new_num = 1;
                $new_data = array('num'=>$new_num,'last_time'=>time());
                $return = true;
            } else {
            	$new_data = array('num'=>$new_num);
                $return = $stat->num >= $max_num ? false : true;
            }
        }
        //更新统计
        $user_stat_mod->findFirst("uid = '$uid' and action = '$action'")->save($new_data);
        return $return;
    } 
	
}
